package com.ctrlcar.giftrecord.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ctrlcar.giftrecord.dto.SendGiftDTO;
import com.ctrlcar.giftrecord.pojo.SendGift;
import com.ctrlcar.giftrecord.pojo.model.Result;
import com.ctrlcar.giftrecord.service.SendGiftService;
import com.ctrlcar.giftrecord.vo.SendGiftVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author:
 * @Description: 送礼模块
 * @DateTime: 2025-03-28 19:31
 **/

@RestController
@RequestMapping("/sendGift")
public class SendGiftController {
    @Resource
    private SendGiftService sendGiftService;


    /**
     *  回礼
     * @param sendGift
     * @return
     */
    @PostMapping
    public Result sendGift(@RequestBody SendGift sendGift){
        //封装送礼记录
        boolean save = sendGiftService.save(sendGift);
       if (save){
           return Result.success();
       }else {
           return Result.error("送礼失败");
       }
    }

    /**
     * 新增送礼记录
     * @param sendGiftDTO
     * @return
     */
    @PostMapping("/add")
    public Result addSendGift(@RequestBody SendGiftDTO sendGiftDTO){
        String save = sendGiftService.addSendGift(sendGiftDTO);
        if (save.equals("失败")){
            //封装送礼记录
            return Result.error("送礼失败");
        }
        return Result.success(save);
    }


    /**
     * 根据用户id和relativeId获取list
     * @param userId
     * @return
     */
    @GetMapping("/listBySendGift")
    public Result<List<SendGift>> listBySendGift(@RequestParam Integer userId,@RequestParam Integer relativeId){
        LambdaQueryWrapper<SendGift> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SendGift::getUserId,userId);
        queryWrapper.eq(SendGift::getRelativeId,relativeId);
        List<SendGift> list = sendGiftService.list(queryWrapper);
        return Result.success(list);
    }


    /**
     * 根据用户ID、礼物名称和地址获取送礼记录
     * @param userId 用户ID
     * @return
     */
    @GetMapping("/listBySendGiftName")
    public Result<List<SendGiftVo>> listBySendGiftName(
            @RequestParam Integer userId,
            @RequestParam(required = false) String sel) {
        return sendGiftService.getAllByUserId(userId,sel);
    }

    /**
     * 删除送礼记录
     * @param SendGiftId
     * @return
     */
    @DeleteMapping
    public Result  deleteSendGift(@RequestParam Integer SendGiftId){
        Result result =  sendGiftService.delectBySendGiftId(SendGiftId);
        return result;
    }
    /**
     * 修改送礼记录
     */
    @PutMapping
    public Result  updateSendGift(@RequestBody SendGift sendGift){
        boolean update = sendGiftService.updateById(sendGift);
        if (update){
            return Result.success("修改成功");
        }
        return Result.error("修改失败");
    }
    /**
     * 根据id查询送礼信息
     */
    @GetMapping
    public Result<SendGift> getSendGift(@RequestParam Integer sendGiftId){
        SendGift sendGift = sendGiftService.getById(sendGiftId);
        return Result.success(sendGift);
    }
}
